var tableIns;
layui.extend({
   dtree:'/layui_ext/dist/dtree'
}).use(['jquery','layer','form','table','dtree'],function (){
    var $ = layui.jquery,
        layer = layui.layer,
        form = layui.form,
        table = layui.table,
        dtree = layui.dtree;

    tableIns = table.render({
        elem:'#userTable',
        url: '/system/selectAllUser',
        title: '用户数据表',
        toolbar: '#userToolBar',
        // height: 'full-230',
        cellMinWidth: 200,
        page: 1,
        limits: [10,20,30],
        limit: 10,
        cols:[[
            // {field: 'userid',title:'ID',align:'center',width:'50'},
            {field: 'loginname',title:'用户姓名',align:'center'},
            {field: 'realname',title:'登录名',align:'center'},
            {field: 'sex',title:'性别',align:'center',templet: function (d){
                    return d.sex=='1'?'<font color="blue">男</font>':'<font color="red">女</font>';
            }},
            // {field: 'pwd',title:'密码',align:'center',templet:function (){
            //     return '*******';
            //  }},
            // {field: 'identity',title:'身份证号',align:'center'},
            {field: 'phone',title:'电话',align:'center'},
            {field: 'address',title:'地址',align:'center'},
            {field: 'position',title:'描述',align:'center'},
            {field: 'available',title:'状态',align:'center',templet: function (d){
                    return d.available=='1'?'<font color="blue">可用</font>':'<font color="red">不可用</font>';
            }},
            {fixed: 'right',title:'操作', toolbar: '#userBar',align:'center',width:'260'}
        ]]
    });

    $("#doSearch").click(function (){
        var params = $("#searchFrm").serialize();
        tableIns.reload({
            url: '/system/selectAllUser?'+params
        });
    });

    table.on("toolbar(userTable)",function (obj){
       switch (obj.event){
           case 'add':
               openAddUser();
               break;
           }
    });

    table.on('tool(userTable)', function (obj){
        var data = obj.data;
        var layEvent = obj.event;
        if(layEvent == 'del'){
            layer.confirm('确认删除【'+data.realname+'】用户吗？',function (){
                $.post("/system/deleteUser",{userid:data.userid},function (res){
                    if(res > 0){
                        layer.msg("用户删除成功！");
                        tableIns.reload();
                    }else{
                        layer.msg("用户删除失败！");
                    }

                });
            });
        }else if(layEvent == 'edit'){
            openUpdateUser(data);
        }else if(layEvent == 'resetUserPwd'){
            layer.confirm('确认是否重置【'+data.realname+'】用户密码？',function (){
                $.post('/system/resetUserPwd',{userid:data.userid},function (res){
                    if(res > 0){
                        layer.msg("密码重置成功！");
                    }else{
                        layer.msg("密码重置失败！");
                    }
                });
            });
        }else if(layEvent == 'selectUserRole'){
            openselectUserRole(data);
        }
    });

    var flag1 = false;
    var flag2 = false;

    function loginname(){
        $.post('/system/queryUserName',{loginname: $("#loginname").val()},function (res){
           if(res > 0){
               layer.msg("登录用户名已存在！");
               flag1 = true;
           }else{
               flag1 = false;
           }
        });
    }

    function identity(){
        $.post('/system/queryUserIdentity',{loginname: $("#identity").val()},function (res){
            if(res > 0){
                layer.msg("身份证号已存在！");
                flag2 = true;
            }else{
                flag2 = false;
            }
        });
    }

    var url;
    var mainIndex;
    function openAddUser(){
        if(flag1 == true || flag1 == true){
            layer.msg("登录名或身份证号已存在！");
            return false;
        }
        mainIndex = layer.open({
            type: 1,
            title: '添加用户',
            content: $("#saveOrUpdateDiv"),
            area:['800px','400px'],
            success:function (index){
                $("#dataFrm")[0].reset();
                url="/system/addUser";
            }
        });
    }

    function openUpdateUser(data){
        mainIndex = layer.open({
            type: 1,
            title: '修改用户',
            content: $("#saveOrUpdateDiv"),
            area:['800px','400px'],
            success:function (index){
                form.val("dataFrm",data);
                url="/system/updateUser";
            }
        });
    }

    form.on('submit(doSubmit)',function (){
       var params = $("#dataFrm").serialize();
       $.post(url,params,function (res){
           layer.msg(res);
           layer.close(mainIndex);
           tableIns.reload();
       });
    });

    function openselectUserRole(data){
        mainIndex = layer.open({
            type: 1,
            title: '分配【'+data.realname+'】的角色',
            content: $("#selectUserRole"),
            area:['800px','400px'],
            btnAlign: 'c',
            height: 'full',
            btn: ['<div class="layui-icon layui-icon-release">确认分配</div>',
                '<div class="layui-icon layui-icon-close">取消分配</div>'],
            yes:function (index,layero){
                var checkStatus = table.checkStatus('roleTable');
                var roleData = checkStatus.data;
                var params = "userid="+data.userid;
                $.each(roleData, function (i, item){
                   params += "&ids="+item.roleid;
                });

                $.post("/system/saveUserRole",params,function (res){
                    if(res > 0){
                        layer.msg("分配成功！");
                    }else{
                        layer.msg("分配失败！");
                    }
                });
            },
            success:function (index){
                var roleTableIns = table.render({
                    elem:'#roleTable',
                    url: '/system/initUserRole?userid='+data.userid,
                    title: '角色列表',
                    cellMinWidth: 100,
                    cols:[[
                        {type: 'radio',fixed:'left'},
                        {field: 'roleid',title:'ID',align:'center'},
                        {field: 'rolename',title:'角色名称',align:'center'},
                        {field: 'roledesc',title:'角色备注',align:'center'}
                    ]]
                });
            }
        });
    }
});